# # coding=utf-8
import openpyxl
import xlrd
import time
import requests
import json
import datetime
import random
import string
from auto_interface.testting import rpb_all_param

class Excel_rpb():
    @classmethod
    def sava_6(self, j, org_id, imep_code, trspModecd,orgArea):
        headers = rpb_all_param.Param.headers
        headers.update({"orgId": str(org_id), "orgArea": orgArea})
        ids = []
        i = 0
        pack_list = []
        while i < j:
            # print(i)
            bill_param = rpb_all_param.Param.bill_param_i
            bill_param.update(
                {"orgId": org_id, "impexpPortcd": imep_code, "grossWt": "5000", "billLodNo": "MMK1001", "voyageNo": "0034W",
                 "shipNameEn": "shipNameEnx" + '%05d' % i, "trspModecd": str(trspModecd)})

            response = Excel_rpb().post_requests(headers=headers,
                                                 url='http://', data=json.dumps(bill_param))
            print(response)
            bill_id = response.get("map").get("id")
            ids.append(str(bill_id))
            i += 1
        print(ids)

    @classmethod
    def sava_6e(self, j, org_id, imep_code, trspModecd, orgArea):
        headers = rpb_all_param.Param.headers
        headers.update({"orgId": str(org_id), "orgArea": orgArea})
        ids = []
        i = 0
        pack_list = []
        while i < j:
            # print(i)
            bill_param = rpb_all_param.Param.bill_param_e
            bill_param.update(
                {"orgId": org_id, "impexpPortcd": imep_code, "grossWt": "5000", "billLodNo": "MMK1001",
                 "voyageNo": "0034W",
                 "shipNameEn": "shipNameEna" + '%05d' % i, "trspModecd": str(trspModecd)})

            response = Excel_rpb().post_requests(headers=headers,
                                                 url='http://', data=json.dumps(bill_param))
            print(response)
            bill_id = response.get("map").get("id")
            ids.append(str(bill_id))
            i += 1
        print(ids)

    def sava_1_5(self,j,org_id,imep_code,inexp_type,ie_type,trspModecd,orgArea,rltEntryNo):
        headers = rpb_all_param.Param.headers
        headers.update({"orgId": str(org_id), "orgArea": orgArea})
        i = 0
        id = []
        pack_list = []
        while i < j:
            ware_param = rpb_all_param.Param.ware_param
            ware_param.update({"orgId": org_id, "impexpPortcd": imep_code, "inExpType": inexp_type, "ieTypecd": ie_type, "rltEntryNo": rltEntryNo,"trspModecd":str(trspModecd),'rmk':'测试0228'})

            response = Excel_rpb().post_requests(headers=headers,url=rpb_all_param.Param.waresave_url, data=json.dumps(ware_param))
            ware_id = response.get("map").get("data")
            id.append(ware_id)

            i += 1
            print(i)
        print(id)


    def save_port(self,j,org_id,imep_code,orgArea):
        print("生成卡口凭证")
        headers = rpb_all_param.Param.headers
        headers.update({"orgId": str(org_id), "orgArea": orgArea})
        port_param = rpb_all_param.Param.port_param
        port_param.update({"orgId": org_id, "impexpPortcd": imep_code, "inExpType": "3", "ieTypecd": "I", "bindTypecd": "", "rmk" : "测试0301"})
        port_param.update({"vehicleNo": "沪F00000"})

        i = 0
        id = []
        while i < j:
            port_save = Excel_rpb().post_requests(headers=headers,
                                                  url=rpb_all_param.Param.portsave_url,
                                                  data=json.dumps(port_param))
            print(port_save)
            port_id = port_save.get("map").get("data")
            port_no = port_save.get("map").get("rltList")[0].get("portNo")
            id.append(str(port_id))
            i += 1
            print(i)
        print(id)

    def post_requests(self, headers, url, data):
        try:
            response = requests.post(url=url, headers=headers, data=data)
            ass_value = response.json()
            return ass_value
        except Exception as E:
            return False, E, "请求异常"

# 生成装车单
Excel_rpb().sava_6e(org_id="103240",
                          imep_code="2248",
                          j=1,
                          trspModecd='2',
                          orgArea="D")
# Excel_rpb().sava_1_5(org_id="103240",
#                           imep_code="2248",
#                           j=1000,
#                     inexp_type='2',
#                     ie_type='I',
#                           trspModecd='2',
#                           orgArea="D",
#                      rltEntryNo='1')
# Excel_rpb().save_port(org_id="103240",
#                           imep_code="2248",
#                           j=1000,
#                           orgArea="D")



# a = []
# i = 9537
# j = 0
# while i < 10537:
#     a.append(str(i))
#     j += 1
#     i += 1
#     print(j)
# print(a)